package com.xjtu.platform.security.controller;

import com.xjtu.platform.common.*;
import com.xjtu.platform.reader.ReaderService;
import com.xjtu.platform.security.dto.AdminLoginDto;
import com.xjtu.platform.security.dto.LoginDto;
import com.xjtu.platform.security.LoginService;
import com.xjtu.platform.security.dto.NewPwdDto;
import com.xjtu.platform.security.dto.TestDto;
import com.xjtu.platform.security.entitiy.Info;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/security")
//@EnableScheduling
////@ConditionalOnProperty(prefix = "scheduling", name = "enabled", havingValue = "true")
public class testLogin {

    @Resource
    private LoginService loginService;

    @Resource
    private CommonService commonService;


    @PostMapping("/login")
    public Result Login(@RequestBody LoginDto loginDto) {
        boolean ok = loginService.checkLogin(loginDto);
        if (ok) {
            String readerId = loginService.getReaderId(loginDto.getReader_code());
            System.out.println(readerId);
            String token = TokenUtils.loginSign(readerId, loginDto.getPwd());
            return Result.success("成功", token);
        }
        return Result.fail(Result.ERR_CODE_SYS, "密码错误");
    }

    @GetMapping("/getToken")
    public Result getToken(@RequestHeader("Token") String token) {
        String reader_id = TokenUtils.getUserInfo(token, commonService).getUserId();
        return Result.success("成功", reader_id);
    }

    @PostMapping("/update_pwd")
    public Result updatePwd(@RequestBody NewPwdDto newPwdDto, @RequestHeader("Token") String token) {
        String reader_id = TokenUtils.getUserInfo(token, commonService).getUserId();
        boolean isUpdate = loginService.updatePwd(newPwdDto, reader_id);
        if (isUpdate) {
            TokenUtils.removeToken(token);
            return Result.success("成功");
        } else {
            return Result.fail(Result.ERR_CODE_SYS, Constants.ERROR_MESSAGE);
        }
    }

    @GetMapping("/get_user_info")
    public Result getUserInfo(@RequestHeader("Token") String token) {
        try {
            String reader_id = TokenUtils.getUserInfo(token, commonService).getUserId();
            Info info = loginService.getInfo(reader_id);
            return Result.success("成功", info);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(Result.ERR_CODE_SYS, Constants.ERROR_MESSAGE);
        }
    }

    @PostMapping("/test")
    public Result test(@RequestPart("fileList") List<MultipartFile> fileList, @RequestPart("json") TestDto dto) throws Exception{
        for (MultipartFile item : fileList){
            InputStream in = item.getInputStream();
            System.out.println(item.getOriginalFilename());
            Date date = new Date();
            Timestamp timestamp = new Timestamp(date.getTime());
            SimpleDateFormat ft = new SimpleDateFormat ("yyyy-MM-dd-hh-mm-ss");
            String time_string = ft.format(timestamp);
            String filename = time_string+'-'+item.getOriginalFilename();
            System.out.println(filename);

            String fileUrl = Constants.ADDRESS_HEAD+filename;
            OutputStream out = new FileOutputStream(fileUrl);
            byte[] b = new byte[10240];
            int len = -1;

            while((len=in.read(b))!=-1){
                out.write(b,0,len);
            }
            out.flush();
            out.close();
            in.close();
        }
        System.out.println(dto.toString());
        return Result.success("s");
    }


    @PostMapping("/admin/login")
    public Result adminLogin(@RequestBody AdminLoginDto adminLoginDto) {
        boolean ok = loginService.checkAdminLogin(adminLoginDto);
        if (ok) {
            String adminId = loginService.getAdminId(adminLoginDto.getAdmin_code());
            System.out.println(adminId);
            String token = TokenUtils.loginSign(adminId, adminLoginDto.getPwd());
            return Result.success("管理员登录成功", token);
        }
        return Result.fail(Result.ERR_CODE_SYS, "密码错误");
    }



//    @PostMapping("/testToken")
//    public Result testToken(@RequestHeader("Token")String token){
//        String user_id = TokenUtils.getUserInfo(token,commonService).getUserId();
//        return Result.success(user_id);
//    }
//

}
